//
//  ApplyHouseDetailController.swift
//  xiaoqiao
//
//  Created by 冷子豪 on 2017/7/31.
//  Copyright © 2017年 冷子豪. All rights reserved.
//

import UIKit

class ApplyHouseDetailController: UITableViewController {
    
    var house: House?

    @IBOutlet weak var numOfCertificate: UITextField!
    @IBOutlet weak var price: UITextField!
    @IBOutlet weak var city: UITextField!
    @IBOutlet weak var houseType: UITextField!
    @IBOutlet weak var payment: UITextField!
    @IBOutlet weak var address: UITextField!
    @IBOutlet weak var area: UITextField!
    
    /// 完成回调属性
    var completionCallBack:(()->())?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 判断 house 是否有值，如果有设置 UI
        if house != nil{
            numOfCertificate.text = house?.numOfCertificate
            price.text = "\(String(describing: (house?.price)!))"
            city.text = house?.cityOfHouse
            houseType.text = house?.houseType
            payment.text = "\(String(describing: (house?.payment)!))"
            address.text = house?.address
            area.text = "\(String(describing: (house?.area)!))"
            
        }

    }
    
    @IBAction func saveHouse(_ sender: Any) {
        // 1. 判断 person 是否为 nil，如果是就是新建
        if house == nil{
            house = House()
        }
        // 2. 用 UI 更新 person 的内容
        house?.userid = user.id
        house?.numOfCertificate = numOfCertificate.text
        house?.price = Double(price.text!)
        //        house?.yearsOfHouse =
        //        house?.marketValue =
        //        house?.valueOfArround =
        
        house?.cityOfHouse = city.text
        house?.houseType = houseType.text
        house?.payment = Double(payment.text!)
        house?.address = address.text
        house?.area = Double(area.text!)
        house?.status = "正常"
        
        
        // 3. 执行闭包回调
        completionCallBack?()

        // 4. 返回上一级界面
        _ = navigationController?.popViewController(animated: true)
    }
}
